package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.CourseTrain;
import com.dingreading.cloud.admin.entity.table.CourseTrainTableDef;
import com.dingreading.cloud.admin.mapper.CourseTrainMapper;
import com.dingreading.cloud.admin.service.CourseTrainService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 叮叮线下培训课程表 服务层实现。
 *
 * @author EDY
 * @since 2025-11-17
 */
@Service
public class CourseTrainServiceImpl extends BaseServiceImpl<CourseTrainMapper, CourseTrain, CourseTrainTableDef> implements CourseTrainService {

    @Override
    protected CourseTrainTableDef getTable() {
        return CourseTrainTableDef.courseTrain;
    }

    @Override
    public long getByTypeIdCnt(String typeUid) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.typeUid.eq(typeUid).or(table.mainTypeUid.eq(typeUid)));
        return getMapper().selectCountByCondition(condition);
    }

    @Override
    public Page<CourseTrain> pageList(PageUtil pageUtil, String mainTypeUid, String typeUid, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(mainTypeUid))
            condition.and(table.mainTypeUid.eq(mainTypeUid));
        if (StringUtils.isNotBlank(typeUid))
            condition.and(table.typeUid.eq(typeUid));

        if (StringUtils.isNotBlank(keyword))
            condition.and(table.name.like(keyword));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public Integer getMaxShowOrder(String typeUid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .where(table.typeUid.eq(typeUid));

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);

        return NepUtil.nullToZero(integer);
    }

    @Override
    public CourseTrain getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public CourseTrain getLtOrderNum(String typeUid, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.typeUid.eq(typeUid))
                .and(table.orderNum.lt(orderNum));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.desc());
        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public boolean updateOrderNum(Long id, Integer curOrderNum) {
        return UpdateChain.of(CourseTrain.class)
                .set(table.orderNum, curOrderNum)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public CourseTrain getGtOrderNum(String typeUid, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.typeUid.eq(typeUid))
                .and(table.orderNum.gt(orderNum));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.desc());
        return getMapper().selectOneByQuery(wrapper);
    }
}
